package com.huluobo.a2206parkdemo.patrol

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.gson.Gson
import com.huluobo.a2206parkdemo.R
import com.huluobo.a2206parkdemo.adapter.PatrolAdapter
import com.huluobo.a2206parkdemo.databinding.ActivityPatrolBinding
import com.huluobo.base.bean.PPointBean
import com.huluobo.base.bean.PatrolBean
import com.huluobo.base.mvvm.BaseActivity
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class PatrolActivity : BaseActivity<ActivityPatrolBinding, PatrolViewModel>() {
    private lateinit var patrolAdapter: PatrolAdapter
    override fun initData() {
        vm.getAllPatrol()
        vm.getAllPatrolSuccess.observe(this) {
            val list = Gson().fromJson(it, PatrolBean::class.java)
            patrolAdapter.data.clear()
            patrolAdapter.data.addAll(list)
            patrolAdapter.notifyDataSetChanged()
        }
    }

    override fun onResume() {
        super.onResume()
        vm.getAllPatrol()
    }

    override fun initView() {
        binding.patrolHeader.headerTitle.text = "巡更管理"
        binding.patrolHeader.headerBack.setOnClickListener { finish() }
        binding.patrolHeader.headerAdd.visibility = View.VISIBLE
        binding.patrolHeader.headerAdd.setOnClickListener {
            val intent = Intent(this, AddPatrolActivity::class.java)
            startActivity(intent)
        }

        binding.patrolRv.layoutManager = LinearLayoutManager(this)
        patrolAdapter = PatrolAdapter()
        binding.patrolRv.adapter = patrolAdapter

        patrolAdapter.setOnItemClickListener { adapter, view, position ->
            Log.i("TagA", "item:${patrolAdapter.data[position]}")
        }
    }
}